<template>
  <div class="dashboard-container">
    <div class="app-container">
      <PageTools>
        <span slot="before">共{{ total }}条记录</span>
        <template slot="after">
          <el-button size="small" type="warning">导入</el-button>
          <el-button size="small" type="danger">导出</el-button>
          <el-button size="small" type="primary" @click="showAddEmployeeDialog = true">新增员工</el-button>
        </template>
      </PageTools>
      <!-- 表格 -->
      <el-card>
        <el-table border :data="employeeList">
          <el-table-column label="序号" sortable="" type="index" />
          <el-table-column label="姓名" sortable="" prop="username" />
          <el-table-column label="头像" align="center">
            <template slot-scope="scope">
              <img v-imgerror="require('@/assets/common/default.jpeg')" :src="scope.row.staffPhoto" style="border-radius: 30%; width: 100px; height: 100px; padding: 5px;">
            </template>
          </el-table-column>
          <el-table-column label="工号" sortable="" prop="workNumber" />
          <!-- <el-table-column label="聘用形式" sortable="" prop="formOfEmployment" /> -->
          <!-- 格式化聘用形式 -->
          <el-table-column label="聘用形式" sortable prop="formOfEmployment" :formatter="formatEmployment" />

          <el-table-column label="部门" sortable="" prop="departmentName" />
          <el-table-column label="入职时间" sortable="" prop="timeOfEntry">
            <template slot-scope="scope">
              {{ scope.row.timeOfEntry | formatDate('yyyy年-MM月-dd日') }}
            </template>
          </el-table-column>
          <el-table-column label="账户状态" sortable="" prop="enableState">
            <template slot-scope="scope">
              <el-switch
                :value="scope.row.enableState === 1"
                active-color="#13ce66"
                inactive-color="#ff4949"
              />
            </template>
          </el-table-column>
          <el-table-column label="操作" sortable="" fixed="right" width="280">
            <template slot-scope="{ row }">
              <el-button type="text" size="small" @click="$router.push(`/employees/detail/${row.id}`)">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small">角色</el-button>
              <el-button type="text" size="small" @click="delEmployee(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <!-- 分页组件 -->
          <el-pagination
            background
            :current-page="pageObj.page"
            :page-sizes="[2, 4, 6, 8, 10]"
            :page-size="pageObj.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-row>

      </el-card>

      <!-- 放置新增组件 -->
      <add-employee :show-dialog.sync="showAddEmployeeDialog" />

    </div>
  </div>
</template>

<script>
import { getEmployeeList, delEmployee } from '@/api/employees'

import addEmployee from './components/add-employee' // 组件：弹出层

import EmployeeEnum from '@/api/constant/employees'

export default {
  // 注册组件
  components: {
    addEmployee
  },
  // 数据
  data() {
    return {
      employeeList: [], // 员工列表

      // 分页对象
      pageObj: {
        page: 1, // 第几页
        size: 8 // 每页数量
      },
      total: 0, // 总页数

      // 新增员工弹出框
      showAddEmployeeDialog: false
    }
  },
  // 钩子
  created() {
    this.getEmployeeList()
  },
  // 方法
  methods: {
    // 分页器方法：
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.pageObj.size = val
      this.getEmployeeList()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.pageObj.page = val
      this.getEmployeeList()
    },

    // 获取员工列表
    async getEmployeeList() {
      const { rows, total } = await getEmployeeList(this.pageObj)
      this.employeeList = rows
      this.total = total
      console.log(this.employeeList)
    },

    // 删除员工
    async delEmployee(id) {
      // console.log(id)

      // 弹出确认提示框
      await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })

      try {
        await delEmployee(id)
        this.$message.success('删除成功')
        this.getEmployeeList() // 重新获取列表
      } catch (error) {
        this.$message.error(error)
      }
    },

    // 格式化聘用形式
    formatEmployment(row, column, cellValue, index) {
      // 要去找 1所对应的值
      const obj = EmployeeEnum.hireType.find(item => item.id === cellValue)
      return obj ? obj.value : '未知'
    }
    // 方法End
  }
}
</script>

<style>

</style>
